package summary;

import java.util.Arrays;

/**
 * @Author: 海琳琦
 * @Date: 2022/8/7 21:38
 */
public class Title135 {

    public static int candy(int[] ratings) {
        int n = ratings.length;
        int[] nums = new int[n];
        int[] nums2 = new int[n];
        Arrays.fill(nums, 1);
        Arrays.fill(nums2, 1);
        //正向循环
        for (int i = 1; i < n; i++) {
            if (ratings[i] > ratings[i - 1]) {
                nums[i] += nums[i - 1];
            }
        }
        for (int i = n - 2; i >= 0; i--) {
            if (ratings[i] > ratings[i + 1]) {
                nums2[i] += nums2[i + 1];
            }
        }
        int count = 0;
        for (int i = 0; i < n; i++) {
            count += Math.max(nums[i], nums2[i]);
        }
        return count;
    }

    public static void main(String[] args) {
        candy(new int[]{1, 0, 2});
    }
}
